//
//  ViewController.swift
//  KeepAliveDemo
//
//  Created by Admin on 2024/10/27.
//

import UIKit
import CoreLocation
import AVFoundation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var taskIdentify: UIBackgroundTaskIdentifier?
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        self.playMusic()
        
        self.taskIdentify =  UIApplication.shared.beginBackgroundTask {
            if let task = self.taskIdentify {
                UIApplication.shared.endBackgroundTask(task)
                self.taskIdentify = UIBackgroundTaskIdentifier.invalid
            }
           
        }
        
        let ss =  CLLocationManager.authorizationStatus().rawValue
       let status = CLLocationManager.authorizationStatus
        
        NotificationCenter.default.removeObserver(self)
        
        NotificationCenter.default.addObserver(self, selector: #selector(enterFore), name: UIApplication.willEnterForegroundNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(enterFore), name: UIApplication.didEnterBackgroundNotification, object: nil)
        "".hasPrefix("http")
    }
    
    @objc
    func enterFore() {
        
    }


    func playMusic() {
        var urlValue = Bundle.main.url(forResource: "bgmusic", withExtension: "mp3")
        var urlStr = urlValue?.absoluteString ?? ""
        if urlStr.hasPrefix("file://") {
            urlStr.substring(from: urlStr.index(urlStr.startIndex, offsetBy: "file://".count))
        }
        let url = URL(fileURLWithPath: urlStr)
       
        if let player = try? AVAudioPlayer(contentsOf: url) {
            player.prepareToPlay()
//          //一直循环播放
            player.numberOfLoops = -1
            player.volume = 0
//            AVAudioSession.sharedInstance().beginConfiguration()
            do {
                try AVAudioSession.sharedInstance().setCategory(.playback)
                try AVAudioSession.sharedInstance().setActive(true)
            } catch  {
                
            }
//            AVAudioSession.sharedInstance().commitConfiguration()
            player.play()
            player.pause()
           
            player.stop()
        }
    }
    
    func locatioin() {
        let locationManager = CLLocationManager()
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager.allowsBackgroundLocationUpdates = true//允许后台刷新
        locationManager.pausesLocationUpdatesAutomatically = false //不允许自动暂停刷新
        locationManager.distanceFilter = kCLDistanceFilterNone;  //不需要移动都可以刷新
        locationManager.delegate = nil
        locationManager.startUpdatingLocation()
        
        locationManager.stopUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.first
        location?.altitude
     
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        
    }
}

